package com.school.aftersalemanagement.controller.ui.main;

import android.app.Fragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.LocationSource;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.school.aftersalemanagement.R;
import com.school.aftersalemanagement.controller.ui.bicyclemanagement.BicycleManagementActivity;
import com.school.aftersalemanagement.controller.ui.scancode.ScanCodeActivity;
import com.school.aftersalemanagement.entity.Bike;
import com.school.aftersalemanagement.global.BaseActivity;
import com.school.aftersalemanagement.service.BicycleService;
import com.school.aftersalemanagement.service.impl.BicycleServiceimpl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by winkyle on 2017/10/3.
 */

public class MapFragment extends Fragment{
    @BindView(R.id.map)
    MapView mMapView;

    private Map map;
    //当前位置

    private BicycleService bicycleService;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main_map, container, false);
        ButterKnife.bind(this, view);
        map = new Map(getActivity(),this);
        mMapView.onCreate(savedInstanceState);// 此方法必须重写
        map.intiMap(mMapView);
        bicycleService = new BicycleServiceimpl(getActivity(),mHandler);
        return view;
    }

    private List<Bike> bikes;
    private int GET_BIKELIST = 1;
    public int SHOW_BIKE = 2;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == GET_BIKELIST) {
                bikes =(List<Bike>)msg.obj;
                ((MainActivity)getActivity()).drawerLayout.closeDrawer(GravityCompat.START);
                map.refreshMarket(bikes);
            }else if(msg.what ==SHOW_BIKE){
                String bikenumber = (String) msg.obj;
                Log.d("BikeNumber",bikenumber);
                bicycleService.searchBike(GET_BIKELIST,bikenumber,"","","","","","");
            }
        }
    };


    @Override
    public void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        mMapView.onDestroy();
    }

    @Override
    public void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        mMapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        mMapView.onPause();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        mMapView.onSaveInstanceState(outState);
    }

    @BindView(R.id.fab_refresh)
    ImageView floatingActionButton;
    private RotateAnimation myAnimation_Rotate;
    @OnClick({R.id.dinwgei,R.id.fab_refresh,R.id.btn_use,R.id.opendrawer,R.id.fab_qr})
    public void onViewClicked(View view) {

        switch (view.getId()) {
            case R.id.dinwgei:
                map.setLocation();
                break;
            case R.id.fab_refresh:
                myAnimation_Rotate = new RotateAnimation(0, 720,
                        Animation.RELATIVE_TO_SELF, 0.5f,
                        Animation.RELATIVE_TO_SELF, 0.5f);
                myAnimation_Rotate.setDuration(500);
                myAnimation_Rotate.setFillEnabled(true);
                myAnimation_Rotate.setFillAfter(true);
                floatingActionButton.startAnimation(myAnimation_Rotate);
                mMapView.getMap().clear();
                bicycleService.getBicycleList(GET_BIKELIST,getLocation(),1);
                break;
            case R.id.btn_use:
                Intent intent = new Intent(getActivity(), BicycleManagementActivity.class);
                intent.putExtra("location",getLocation());
                getActivity().startActivityForResult(intent, 200);
                break;
            case R.id.opendrawer:
                ((MainActivity)getActivity()).drawerLayout.openDrawer(GravityCompat.START);
                break;
            case R.id.fab_qr:
                Intent intenti = new Intent(getActivity(),ScanCodeActivity.class);
                getActivity().startActivityForResult(intenti, 200);
                break;
        }
    }

    public Handler getmHandler() {
        return mHandler;
    }

    public String getLocation() {
        return map.getLocation();
    }

    public void getBicycleList() {
        bicycleService.getBicycleList(GET_BIKELIST,getLocation(),1);
    }

    public void mainTenance(String number) {
        bicycleService.mainTenance(0,number);
    }

    public void setRemark(String number, String markstring) {
        bicycleService.setRemark(0,number,markstring);
    }

    public void serviceUnlock(String number) {
        bicycleService.serviceUnlock(0,number);
    }

    public void solveProblem(String number) {
        bicycleService.solveProblem(0,number);
    }

    public void lockBike(String number, int status) {
        bicycleService.lockBike(0,number,status);
    }
}
